ls: Add mode to print filenames as binary
authorColin Walters <walters@verbum.org>
Fri, 6 Apr 2012 19:10:20 +0000 (15:10 -0400)
committerColin Walters <walters@verbum.org>
Fri, 6 Apr 2012 19:10:20 +0000 (15:10 -0400)
This is useful to extract the file listing to another program.

src/ostree/ot-builtin-ls.c

index 30480b05c489c52a2de5c78578f4c5c5b8ec726e..5dd29e8142368b3ff82d2424736d91408844aa98 100644 (file)
 static gboolean recursive;
 static gboolean checksum;
 static gboolean xattrs;
+static gboolean opt_nul_filenames_only;
 
 static GOptionEntry options[] = {
   { "recursive", 'R', 0, G_OPTION_ARG_NONE, &recursive, "Print directories recursively", NULL },
   { "checksum", 'C', 0, G_OPTION_ARG_NONE, &checksum, "Print checksum", NULL },
   { "xattrs", 'X', 0, G_OPTION_ARG_NONE, &xattrs, "Print extended attributes", NULL },
+  { "nul-filenames-only", 0, 0, G_OPTION_ARG_NONE, &opt_nul_filenames_only, "Print only filenames, NUL separated", NULL },
+  { NULL }
 };
 
 static void
-print_one_file (GFile     *f,
-                GFileInfo *file_info)
+print_one_file_text (GFile     *f,
+                     GFileInfo *file_info)
 {
   GString *buf = NULL;
   char type_c;
@@ -118,6 +121,31 @@ print_one_file (GFile     *f,
   g_string_free (buf, TRUE);
 }
 
+static void
+print_one_file_binary (GFile     *f,
+                       GFileInfo *file_info)
+{
+  const char *path;
+
+  if (!ostree_repo_file_ensure_resolved ((OstreeRepoFile*)f, NULL))
+    g_assert_not_reached ();
+
+  path = ot_gfile_get_path_cached (f);
+
+  fwrite (path, 1, strlen (path), stdout);
+  fwrite ("\0", 1, 1, stdout);
+}
+
+static void
+print_one_file (GFile     *f,
+                GFileInfo *file_info)
+{
+  if (opt_nul_filenames_only)
+    print_one_file_binary (f, file_info);
+  else
+    print_one_file_text (f, file_info);
+}
+
 static gboolean
 print_directory_recurse (GFile    *f,
                          GError  **error)